package com.zkthink.ceres.applet.order.controller;

import cn.hutool.extra.servlet.ServletUtil;
import com.zkthink.base.R;
import com.zkthink.ceres.applet.common.domin.PageDate;
import com.zkthink.ceres.applet.common.domin.vo.PageDateTypeIdVo;
import com.zkthink.ceres.applet.order.domain.vo.*;
import com.zkthink.ceres.applet.order.service.AppletProductOrderService;
import com.zkthink.ceres.applet.order.service.AppletServerOrderService;
import com.zkthink.context.BaseContextHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author cmz
 * @Date 2024-04-30 10:32
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/applet/serverOrder")
@Api(value = "appletOrder", tags = "小程序订单")
public class AppletServerOrderController {
    @Autowired
    private AppletServerOrderService appletServerOrderService;
    
    @Autowired
    private AppletProductOrderService appletProductOrderService;
    

    /**
     * 服务或产品下单，将订单信息初始化
     * @param placeServerOrderVo
     * @return
     */
    @ApiOperation(value = "服务或产品下单", notes = "服务或产品下单")
    @PostMapping(value = "/placeServerOrder")
    public R placeServerOrder(HttpServletRequest request,@RequestBody PlaceServerOrderVo placeServerOrderVo) {
        String ip = ServletUtil.getClientIP(request);
        placeServerOrderVo.setIp(ip);
        return appletServerOrderService.placeServerOrder(placeServerOrderVo);
    }

    @ApiOperation("服务订单支付")
    @PostMapping("/payOrder/{orderId}")
    public R<Object> payOrder(HttpServletRequest request, @PathVariable("orderId") Integer orderId) {
        Long mid = BaseContextHandler.getUserId();
        return appletServerOrderService.payOrder(mid, orderId, ServletUtil.getClientIP(request));
    }

    /*@ApiOperation("服务订单设置服务理疗车或门店")
    @PostMapping("/setServerStoreOrVehicles")
    public R setServerStoreOrVehicles(@RequestBody SetServerStoreOrVehiclesVo setServerStoreOrVehiclesVo) {
        
        return appletServerOrderService.setServerStoreOrVehicles(setServerStoreOrVehiclesVo);
    }*/

    /**
     * 设置配送方式
     * @param setDeliveryMethod
     * @return
     */
    @ApiOperation(value = "设置配送方式", notes = "设置配送方式")
    @PostMapping(value = "/setDeliveryMethod")
    public R setDeliveryMethod(@RequestBody SetDeliveryMethod setDeliveryMethod) {
        if (setDeliveryMethod.getOrderType()==1){
            //产品
            return appletProductOrderService.setDeliveryMethod(setDeliveryMethod);
        }else if (setDeliveryMethod.getOrderType()==0){
            //服务
            SetServerStoreOrVehiclesVo setServerStoreOrVehiclesVo = new SetServerStoreOrVehiclesVo();
            //setServerStoreOrVehiclesVo.setAddressId(setDeliveryMethod.getAddressId());
            setServerStoreOrVehiclesVo.setServerChannel(setDeliveryMethod.getDeliveryMethod());
            setServerStoreOrVehiclesVo.setServerOrderId(setDeliveryMethod.getId());
            setServerStoreOrVehiclesVo.setStoreId(setDeliveryMethod.getStoreId());
            setServerStoreOrVehiclesVo.setVehiclesId(setDeliveryMethod.getStoreId());//车辆id

            return appletServerOrderService.setServerStoreOrVehicles(setServerStoreOrVehiclesVo);
        }
        return R.fail("参数值错误");

    }

    /**
     * 设置产品配送方式
     * @param setProductOrderVo
     * @return
     */
//    @ApiOperation(value = "设置产品配送方式", notes = "设置产品配送方式")
//    @PostMapping(value = "/setProductDeliveryMethod")
    public R setProductDeliveryMethod(@RequestBody SetProductOrderVo setProductOrderVo) {
        //产品
        SetDeliveryMethod setDeliveryMethod = new SetDeliveryMethod();
        setDeliveryMethod.setId(setProductOrderVo.getId());
        setDeliveryMethod.setOrderType(1);
        setDeliveryMethod.setAddressId(setProductOrderVo.getAddressId());
        setDeliveryMethod.setDeliveryMethod(setProductOrderVo.getDeliveryMethod());
        setDeliveryMethod.setStoreId(setProductOrderVo.getStoreId());
        return appletProductOrderService.setDeliveryMethod(setDeliveryMethod);
    }
    
    
    @ApiOperation("服务订单确认")
    @PostMapping("/serverOrderConfirm")
    public R serverOrderConfirm(@RequestBody OrderStatusVo orderStatusVo) {
        if (orderStatusVo.getOrderStatus()==null){
            orderStatusVo.setOrderStatus(3);//确认后订单状态更改为待服务
        }
        return appletServerOrderService.serverOrderConfirm(orderStatusVo);
    }

    @ApiOperation("服务订单进行(开始服务/核销)")
    @GetMapping("/serverOrderStart/{orderCode}")
    public R serverOrderStart(@PathVariable("orderCode") String orderCode ) {
        return appletServerOrderService.serverOrderStart(orderCode);
    }

    @ApiOperation("服务订单已完成(服务完成)")
    @PostMapping("/serverOrderComplete")
    public R serverOrderComplete(@RequestBody OrderStatusVo orderStatusVo) {
        if (orderStatusVo.getOrderStatus()==null){
            orderStatusVo.setOrderStatus(5);//调整服务订单的状态为已完成
        }
        return appletServerOrderService.serverOrderComplete(orderStatusVo);
    }

    @ApiOperation("用户签字确认")
    @PostMapping("/ServerOrderSignatureConfirmation")
    public R ServerOrderSignatureConfirmation(@RequestBody ServerOrderSignatureConfirmationVo serverOrderSignatureConfirmationVo) {
        return appletServerOrderService.ServerOrderSignatureConfirmation(serverOrderSignatureConfirmationVo);
    }

    @ApiOperation("查询服务订单列表")
    @PostMapping("/findServerOrderList")
    public R findServerOrderList(@RequestBody PageDate pageDate) {
        return appletServerOrderService.findServerOrderList(pageDate);
    }

    @ApiOperation("取消支付")
    @PostMapping("/cancelPay")
    public R cancelPay(@RequestBody OrderStatusVo orderStatusVo) {
        return appletServerOrderService.cancelPay(orderStatusVo);
    }
}
